﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ControlCenter.ApplicationService;
using ControlCenter.Model;
using ControlCenter.Model.BizEntities;
using ControlCenter.Model.WebApiDto;
using ControlCenterService.WebApi.Internal;
using Microsoft.AspNetCore.Mvc;
using MingYuanyun.WebApi.Abstractions;

namespace ControlCenterService.WebApi.Controllers
{
    /// <summary>
    /// 机器人Api
    /// </summary>
    [Route("[controller]")]
    public class RobotController : BasicController
    {
        private readonly IRobotService _robotService;

        /// <summary>
        /// 类初始化
        /// </summary>
        /// <param name="robotService"></param>
        public RobotController(IRobotService robotService)
        {
            _robotService = robotService;
        }

        /// <summary>
        /// 获取机器人列表（使用者：控制中心）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HttpRequestResult<PagedData<RobotInfoDto>>> Query(RobotQueryDto paramDto)
        {
            return OkHttpRequestResult(await _robotService.Query(paramDto));
        }

        /// <summary>
        /// 机器人注册/更新状态，如果数据库不存在机器人信息，则添加（使用者：ROBOT）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ClientApiFilter]
        public async Task<HttpRequestResult<bool>> UpdateStatus([FromBody]RobotUpdateStatusParamDto paramDto)
        {
            return OkHttpRequestResult(await _robotService.UpdateStatus(paramDto));
        }

        /// <summary>
        /// 定时刷新机器人运行状态，运行状态的机器人或应用30秒内没有心跳/活动时将其状态更新为离线/未运行（使用者：任务调度服务）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("RefreshRobotWorkStatus")]
        public async Task<HttpRequestResult> RefreshRobotWorkStatus()
        {
            await _robotService.RefreshWorkStatus();
            return OkHttpRequestResult();
        }

        /// <summary>
        /// 启用/停用机器人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("SwitchRobot")]
        public async Task<HttpRequestResult> SwitchRobot(SwitchRobotRequestDto paramDto)
        {
            var ret = await _robotService.SwitchRobot(paramDto);
            if (ret.Item1)
            {
                return OkHttpRequestResult();
            }
            else
            {
                return ErrorHttpRequestResult(ret.Item2);
            }
        }

        /// <summary>
        /// 删除机器人，运行状态的不能删除（包括机器人和应用包）
        /// </summary>
        /// <param name="robotId">机器人ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("DeleteRobot")]
        public async Task<HttpRequestResult> DeleteRobot(Guid robotId)
        {
            var ret = await _robotService.DeleteRobot(robotId);
            if (ret.Item1)
            {
                return OkHttpRequestResult();
            }
            else
            {
                return ErrorHttpRequestResult(ret.Item2);
            }
        }
    }
}
